package _0704_Binary_Search;

/**
 * Date: 2020/9/21 9:08
 * Content:
 * 执行用时：0 ms, 在所有 Java 提交中击败了100.00%的用户
 * 内存消耗：39.3 MB, 在所有 Java 提交中击败了66.60%的用户
 */
public class Solution {

    public int search(int[] nums, int target) {

        int l = 0, r = nums.length - 1;
        while (l < r) {

            int mid = (r - l) / 2 + l;
            if (nums[mid] >= target) r = mid;
            else l = mid + 1;
        }
        if (nums[r] == target) return r;
        return -1;
    }

    public static void main(String[] args) {

        int[] nums = {-1, 0, 3, 5, 9, 12};
        int target = 9;
        System.out.println((new Solution()).search(nums, target));
    }
}
